package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderExecutePart;
import com.koron.order.common.mapper.OrderExecutePartMapper;
import com.koron.order.common.service.OrderExecutePartService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (OrderExecutePart)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-03 16:32:17
 */
@Service("orderExecutePartService")
public class OrderExecutePartServiceImpl implements OrderExecutePartService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderExecutePart orderExecutePart) {

        if (StringUtils.isEmpty(orderExecutePart.getId())) {
            orderExecutePart.setId(CodeTools.getCode32());
        }

        int i = factory.getMapper(OrderExecutePartMapper.class).insert(orderExecutePart);
        return i;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderExecutePart> orderExecutePartList) {

        //先根据工单执行id删除,再删除
        if (CollectionUtils.isEmpty(orderExecutePartList)) {
            return 0;
        }

        OrderExecutePartMapper mapper = factory.getMapper(OrderExecutePartMapper.class);
        mapper.deleteByOrderExecuteId(orderExecutePartList.get(0).getExecuteId());
        orderExecutePartList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });
        return mapper.insertBatch(orderExecutePartList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderExecutePart orderExecutePart) {
        int i = factory.getMapper(OrderExecutePartMapper.class).update(orderExecutePart);
        return i;
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = factory.getMapper(OrderExecutePartMapper.class).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        int i = factory.getMapper(OrderExecutePartMapper.class).deleteByOrderId(orderId);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByOrderExecuteId")
    public int deleteByOrderExecuteId(SessionFactory factory, String executeId) {
        return factory.getMapper(OrderExecutePartMapper.class).deleteByOrderExecuteId(executeId);
    }

    @Override
    @TaskAnnotation("deleteOrderExecutePart")
    public int deleteOrderExecutePart(SessionFactory factory, String orderId, String executeId) {
        return factory.getMapper(OrderExecutePartMapper.class).deleteOrderExecutePart(orderId, executeId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderExecutePart queryById(SessionFactory factory, String id) {

        OrderExecutePart orderExecutePart = factory.getMapper(OrderExecutePartMapper.class).queryById(id);

        return orderExecutePart;
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, OrderExecutePart orderExecutePart, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<OrderExecutePart> list = factory.getMapper(OrderExecutePartMapper.class).queryList(orderExecutePart);

        return new PageInfo(list);
    }

}